<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: editor.php 151 2012-02-01 01:08:39Z info@sah-company.com $
 */

/**
 * Kalaisoo Editor Controller Class.
 *
 * This is the editor controller of our framework. It has methods and views to let users add,
 * edit and update or delete beans.
 *
 * @package Kalaisoo
 * @subpackage Controller
 * @author $Author: info@sah-company.com $
 * @see sys/core/refused.php class Controller
 */
class Editor_Controller extends Controller
{
	/**
	 * the currently loaded bean.
	 *
	 * @var Model
	 */
	public $record;
	
	/**
	 * the currently loaded entry accompaning this bean.
	 *
	 * @var Model
	 */
	public $entry;
	
	/**
	 * name of the field to group grid queries
	 *
	 * @var string
	 */
	public $groupField = '';
	
	/**
	 * name of the view for grid queries
	 *
	 * @var string
	 */
	public $viewName = '';
	
	/**
	 * where clause for grid queries
	 *
	 * @var string
	 */
	public $whereClause = '';
	
	/**
	 * name of the order clause for grid queries
	 *
	 * @var string
	 */
	public $orderClause = '';
	
	/**
	 * the current offset for paged queries
	 *
	 * @var int
	 */
	public $limitOffset = 0;
	
	/**
	 * the current limit for paged queries
	 *
	 * @var int
	 */
	public $limitFind = 24;
	
	/**
	 * id of the previous record
	 *
	 * @var int
	 */
	public $prev_id = 0;
	
	/**
	 * id of the next record
	 *
	 * @var int
	 */
	public $next_id = 0;

	/**
	 * constructs a new Editor_Controller and initializes it.
	 */
	public function __construct()
	{
		parent::__construct();
		$this->init();
	}
	
	/**
	 * checks if $record has the method and delegates it there, else look for plugin.
	 *
	 * @param string $method
	 * @param array $params
	 * @return mixed
	 */
	public function __call($method, array $params = array())
	{
		$type = $params[0];
		$id = $params[1];
		$this->loadEnvironment($type, $id);
		if (method_exists('Model_'.ucfirst($type), $method)) {
			$this->record->setController($this);
			return $this->record->$method();
		}
		parent::__call($method, $params);
	}
	
	/**
	 * starts a session and loads needed stuff.
	 *
	 * Start a session, authenticate the user or redirect, load needed stuff and prepare
	 * the editor in general.
	 */
	protected function init()
	{
		session_start();
		$_SESSION['lastActivity'] = time();
		$this->registry->cache->setLifetime(0);
		$this->registry->loader->lib(array('redbean', 'permission', 'gestalt'));
		$this->registry->loader->model(array('user', 'entry'));
		$this->registry->loader->helper(array('url', 'textile'));
		$this->registry->loader->language('editor');
		$this->permission = new Permission();		
		$this->template = new Template();
		$this->input = new Input();
		$this->user = R::dispense('user');
		$this->data['scripts'][] = 'jquery/jquery-1.6.2.min';
		$this->data['scripts'][] = 'core';
		$this->data['scripts'][] = 'editor';
		$this->auth();
		setlocale(LC_TIME, $this->registry->config['locale'][$this->registry->language]);
		$this->registry->addBreadcrumb(__('home'), sprintf('%s/user/index/', $this->registry->language));
	}
	
	/**
	 * checks for a valid user session and loads the user bean or redirects to login.
	 *
	 * If the current session is authenticated the current user is loaded.
	 * Otherwise client is redirected to login with the current url as a referer.
	 */
	protected function auth()
	{
		if ( ! $this->user->isAuth()) {
			return $this->redirect(ahref(sprintf('%s/user/login/?goto=%s', $this->registry->language, urlencode($this->registry->router->url()))));
		}
		$this->user = R::load('user', $_SESSION['auth']);
		$this->data['user'] = $this->user;
	}
	
	/**
	 * graphs the post data into a bean structure and imports it into the current bean.
	 *
	 * This will use Redbean Cooker to graph a bean which is imported into our current model.
	 * This will usually be called before updating, adding and so on.
	 */
	protected function graph()
	{
		$type = $this->record->getMeta('type');
		//$cooker = new Redbean_Cooker();
		//$cooker->setToolbox(R::$toolbox);
		//$this->record->import($cooker->graph($this->input->$type));
		//$this->record->import(R::graph($this->input->$type)); // new code since RB3.6beta
		$this->record = R::graph($this->input->$type);//, true); // new code since RB3.0.1
	}
	
	/**
	 * loads the beans model file and returns true.
	 *
	 * Load or dispense a bean of $type. When $id is not 0 (zero) then the bean will be
	 * loaded otherwise it will be dispensed.
	 * The bean is pushed to our view data.
	 *
	 * @param string $type
	 * @param mixed $id
	 * @return bool
	 */
	protected function loadEnvironment($type, $id = 0)
	{
		// load the model and its language file
		$this->registry->loader->model($type, $load_language = true);
		$this->entry = R::dispense('entry');
		// dispense or load a bean and give it to view
		if ( ! $id) {
			$this->record = R::dispense($type);
		} else {
			$this->record = R::load($type, $id);
			$entries = R::related($this->record, 'entry');
			if ( ! empty($entries)) $this->entry = array_shift($entries);
		}
		$this->entry->setController($this);
		$this->entry->setRecord($this->record);
		$this->entry->setMeta('tainted', true);
		$this->data['record'] = $this->record;
		$this->data['entry'] = $this->entry;
		$this->data['gestalt'] = new Gestalt($this->record);
		return true;
	}
	
	/**
	 * adds breadcrumbs for editor/type/record.
	 *
	 * @param string $action
	 */
	public function addBreadcrumbs($action = 'view')
	{
		$lng = $this->registry->language;
		$type = $this->record->getMeta('type');
		$shortinfo = ( ! $this->record->getId()) ? __($type.'_new') : $this->record->view();
		$this->registry->addBreadcrumb(__('index'), sprintf('%s/editor/index/', $lng));
		$this->registry->addBreadcrumb(__($type), sprintf('%s/editor/index/%s/', $lng, $type));
		$this->registry->addBreadcrumb($shortinfo, sprintf('%s/editor/%s/%s/%s/%s/%s/', $lng, $action, $type, $this->record->getId(), $this->limitOffset, $this->limitFind));
	}
	
	/**
	 * adds possible toolbar actions and url pattern and pushes them to the view data.
	 *
	 * The second parameter can be an array of actions to surpress in the toolbar.
	 *
	 * @param string $action
	 * @param array $excluded
	 * @param bool $skipModel
	 */
	public function toolbarActions($action = 'view', array $excluded = array(), $skipModel = false)
	{
		$this->data['toolbar']['current'] = $action;
		$this->data['toolbar']['url'] = sprintf('%s/editor/%%s/%s/%s/%s/%s/', $this->registry->language, $this->record->getMeta('type'), $this->record->getId(), $this->limitOffset, $this->limitFind);
		$actions = $this->permission->actions($this->user, $this->record->getMeta('type'));
		foreach ($excluded as $action) {
			if (isset($actions[$action])) unset($actions[$action]);
		}
		if ( ! $skipModel) $this->data['model']['actions'] = $this->record->actions();
		$this->data['toolbar']['actions'] = $actions;
	}
	
	/**
	 * establishes a relation betweend the given beans.
	 *
	 * @param string $master
	 * @param mixed $master_id
	 * @param string $slave
	 * @param mixed $slave_id
	 */
	public function associate($master, $master_id, $slave, $slave_id)
	{
		if ( ! $master_id) {
			echo __('editor_assoc_save_first');
			return;
		}
		$this->loadEnvironment($master, $master_id);
		$bean = R::load($slave, $slave_id);
		R::associate($this->record, $bean);
		// output
		$class = 'unassociate';
		$related_help = 'editor_unrelated_help';
		$urlMask = '%s/editor/unassociate/%s/%s/%s/%s';
		$itemId = md5($this->record->getMeta('type').'_'.$this->record->getId().'_'.$bean->getMeta('type').'_'.$bean->getId());
		$url = ahref(sprintf($urlMask, $this->registry->instance()->language, $this->record->getMeta('type'), $this->record->getId(), $bean->getMeta('type'), $bean->getId()));
		echo '<a href="'.$url.'" class="'.$class.'" title="'.__($related_help).'" data-target="'.$itemId.'">'.$bean->view('shortinfo').'</a>';
	}
	
	/**
	 * breaks a relation betweend the given beans.
	 *
	 * @param string $master
	 * @param mixed $master_id
	 * @param string $slave
	 * @param mixed $slave_id
	 */
	public function unassociate($master, $master_id, $slave, $slave_id)
	{
		if ( ! $master_id) {
			echo __('editor_assoc_save_first');
			return;
		}
		$this->loadEnvironment($master, $master_id);
		$bean = R::load($slave, $slave_id);
		R::unassociate($this->record, $bean);
		// output
		$class = 'associate';
		$related_help = 'editor_related_help';
		$urlMask = '%s/editor/associate/%s/%s/%s/%s';
		$itemId = md5($this->record->getMeta('type').'_'.$this->record->getId().'_'.$bean->getMeta('type').'_'.$bean->getId());
		$url = ahref(sprintf($urlMask, $this->registry->instance()->language, $this->record->getMeta('type'), $this->record->getId(), $bean->getMeta('type'), $bean->getId()));
		echo '<a href="'.$url.'" class="'.$class.'" title="'.__($related_help).'" data-target="'.$itemId.'">'.$bean->view('shortinfo').'</a>';
	}
	
	/**
	 * sets the filter for this model and redirects to index.
	 *
	 * @param string $type
	 * @param string $filter
	 */
	public function filter($type = null, $filter = '')
	{
		$_SESSION['filter'][$type] = $filter;
		$this->redirect(ahref(sprintf('%s/editor/index/%s/', $this->registry->language, $type)));
	}
	
	/**
	 * find all beans tagged with the search keywords given in q.
	 *
	 * This will lookup all beans which are related to the search words given in q.
	 * Found beans will be grouped and sorted and then displayed in a certain view.
	 *
	 * @param string $q
	 *
	 */
	public function find($q = '')
	{
		if ($this->input->q) $this->redirect(ahref(sprintf('%s/editor/find/%s/', $this->registry->language, urlencode($this->input->q))));
		$pattern = array(
			' ',
			', '
		);
		$replace = array(
			',',
			','
		);
		$tags = str_replace($pattern, $replace, $q);
		$beanTypesToSearch = array_keys($this->permission->domains($this->user, 'view'));
		$this->registry->loader->model($beanTypesToSearch, true);
		$results = array();
		foreach ($beanTypesToSearch as $beanType) {
			$results[$beanType] = R::tagged($beanType, $tags);
		}
		// url pattern for (adding) record of type n
		$this->data['urlPattern'] = $this->registry->language.'/editor/view/%s/%s/';
		$this->data['foundBeansGrouped'] = $results;
		$this->data['q'] = $q;
		if ( ! $this->registry->request->isAjax()) {
			// add breadcrumb
			$this->registry->addBreadcrumb(__('search'), sprintf('%s/editor/find/', $this->registry->language));
			if ($q) {
				$this->registry->addBreadcrumb($q, sprintf('%s/editor/find/%s', $this->registry->language, urlencode($q)));
			}
			$this->template->view('shared/editor/find', $this->data);
			return;
		}
		$this->template->view('shared/editor/shortinfos', $this->data);
		return;
	}
		
	/**
	 * displays a list of all domains/records the user can use.
	 *
	 * This will output a page where client may pick a certain model to work with
	 * in the editor.
	 * If a certain type is picked one of many view-styles can be used to browse the
	 * beans of that type.
	 *
	 * @param string $type
	 * @param int $limitOffset
	 * @param int $limitFind
	 */
	public function index($type = null, $limitOffset = 0, $limitFind = 24)
	{
		if ( ! $this->permission->granted($this->user, 'index', 'editor')) {
			return $this->error(__('error_no_permission'));
		}
		$this->data['limitOffset'] = $this->limitOffset = $limitOffset;
		$this->data['limitFind'] = $this->limitFind = $limitFind;
		// add breadcrumb
		$this->registry->addBreadcrumb(__('index'), sprintf('%s/editor/index/', $this->registry->language));
		//if ($type !== null && $id) return $this->view($type, $id);
		if ($type !== null) return $this->grid($type);
		// url pattern for (adding) record of type n
		$this->data['urlPattern'] = $this->registry->language.'/editor/index/%s/';
		// find all domains where user can add
		$this->data['menu'] = $this->permission->domains($this->user, 'add');
		$this->template->view('shared/editor/create', $this->data);
	}
	
	/**
	 * Show all records paginated using a chooseable view style.
	 *
	 * @param string $type
	 * @param string $action
	 */
	protected function grid($type, $action = 'grid')
	{
		if ($this->permission->granted($this->user, 'view', $type)) {
			$this->data['type'] = $type;
		}
		$this->loadEnvironment($type);
		// add breadcrumb
		$this->registry->addBreadcrumb(__($type), sprintf('%s/editor/index/%s/', $this->registry->language, $type));
		//$this->data['records'] = R::find($type);
		$this->loadRecords();
		// url pattern for (adding) record of type n
		$this->data['urlPatternView'] = $this->registry->language.'/editor/view/%s/%s/%s/%s/';
		$this->data['urlPatternFilter'] = $this->registry->language.'/editor/filter/'.$this->record->getMeta('type').'/%s/';
		$this->toolbarActions($action, array('index', 'edit', 'view', 'delete'), true);
		// find all domains where user can view
		$this->data['menu'] = $this->permission->domains($this->user, 'view');
		$this->data['model']['filters'] = $this->record->filters();
		if ( ! $this->registry->request->isAjax()) {
			$this->template->view('shared/editor/grid', $this->data);
		} else {
			//echo 'Loaded another set of data...';
			$this->template->view('shared/editor/records', $this->data);
		}
	}
	
	/**
	 * runs an sql query to get all beans and its related tupels as an assoc array.
	 *
	 * @see Controller::$data, Model::describe()
	 * @return bool
	 */
	protected function loadRecords()
	{
		$this->describeAndMakeView();
		$this->buildWhereClause();
		$sql = <<<SQL
		
			SELECT * FROM {$this->viewName}
			{$this->whereClause}
			GROUP BY {$this->groupField}
			ORDER BY {$this->orderClause}
			LIMIT :limitOffset, :limitFind

SQL;
		$values = array(
			':limitOffset' => (int)$this->limitOffset,
			':limitFind' => (int)$this->limitFind
		);
		try {
			//R::debug(true);
			$this->data['records'] = R::getAll($sql, $values);
			//R::debug(false);
			return true;
		} catch (Exception $e) {
			$this->data['records'] = array();
			return false;
		}
	}
	
	/**
	 * set the actions for prev and next bean when limitFind is 1
	 *
	 */
	protected function sniffPrevNext()
	{
		$this->describeAndMakeView();
		$urlPatternPagination = '{language}/editor/%s/%s/%s/%s/%s/';
		
		$this->sniffPrev();
		$this->data['urlPrevPage'] = sprintf($urlPatternPagination, $this->data['toolbar']['current'], $this->record->getMeta('type'), $this->prev_id, max(0, $this->limitOffset-1), $this->limitFind);
		
		$this->sniffNext();
		$this->data['urlNextPage'] = sprintf($urlPatternPagination, $this->data['toolbar']['current'], $this->record->getMeta('type'), $this->next_id, $this->limitOffset+1, $this->limitFind);
	}
	
	/**
	 * sniff for the prev record
	 */
	protected function sniffPrev()
	{
		$limitOffset = max(0, $this->limitOffset - 1);
		$this->buildWhereClause();
		$sql = <<<SQL

			SELECT {$this->groupField} FROM {$this->viewName}
			{$this->whereClause}
			GROUP BY {$this->groupField}
			ORDER BY {$this->orderClause}
			LIMIT :limitOffset, :limitFind

SQL;
		$values = array(
			':limitOffset' => (int)$limitOffset,
			':limitFind' => 1
		);
		try {
			$this->prev_id = R::getCell($sql, $values);
			if ( ! $this->prev_id) $this->prev_id = $this->record->getId();
			return true;
		} catch (Exception $e) {
			$this->prev_id = $this->record->getId();
			return false;
		}		
	}
	
	/**
	 * sniff for the next record
	 */
	protected function sniffNext()
	{
		$limitOffset = $this->limitOffset + 1;
		$sql = <<<SQL

			SELECT {$this->groupField} FROM {$this->viewName}
			{$this->whereClause}
			GROUP BY {$this->groupField}
			ORDER BY {$this->orderClause}
			LIMIT :limitOffset, :limitFind

SQL;
		$values = array(
			':limitOffset' => (int)$limitOffset,
			':limitFind' => 1
		);
		try {
			$this->next_id = R::getCell($sql, $values);
			if ( ! $this->next_id) $this->next_id = $this->record->getId();
			return true;
		} catch (Exception $e) {
			$this->next_id = $this->record->getId();
			return false;
		}		
	}
	
	/**
	 * sets the whereClause based on the filter of a model.
	 *
	 * The current filter for the bean is stored in the user session.
	 */
	protected function buildWhereClause()
	{
		$this->whereClause = '';
		$this->data['model']['filter'] = '';
		if ( ! $filters = $this->record->filters()) return;
		// model has filters, set the session var and build the where clause
		if ( ! isset($_SESSION['filter'][$this->record->getMeta('type')])) {
			$_SESSION['filter'][$this->record->getMeta('type')] = $this->record->filterToken();
		}
		$this->data['model']['filter'] = $_SESSION['filter'][$this->record->getMeta('type')];
		$this->whereClause = 'WHERE '.$filters[$_SESSION['filter'][$this->record->getMeta('type')]]['where'];
	}
	
	/**
	 * analyzes the bean and creates a view
	 */
	protected function describeAndMakeView()
	{
		$this->viewName = 'grid'.$this->record->getMeta('type');
		$this->record->describe($this->record->getMeta('type'), $this->viewName);
		$this->data['view']['tables'] = $this->record->tables();
		$this->data['view']['joins'] = $this->record->joins();
		$this->data['view']['fields'] = $this->record->fields();
		$this->groupField = $this->record->getMeta('type').'_id';
		if ( ! $this->record->orderClause()) {
			$this->orderClause = $this->groupField.' ASC';
		} else {
			$this->orderClause = $this->record->orderClause();
		}
	}
	
	/**
	 * displays a form where user can pick a bean type to create.
	 *
	 * @deprecated
	 * @param string $type
	 */
	public function create($type = 'entry')
	{
		if ( ! $this->permission->granted($this->user, 'create', 'editor')) {
			return $this->error(__('error_no_permission'));
		}
		// url pattern for (adding) record of type n
		$this->data['urlPattern'] = $this->registry->language.'/editor/add/%s';
		// find all domains where user can add
		$this->data['menu'] = $this->permission->domains($this->user, 'add');
		$this->template->view('shared/editor/create', $this->data);
	}
	
	/**
	 * displays a form to add a bean.
	 *
	 * @param string $type
	 * @param mixed $id
	 */
	public function add($type, $id = 0)
	{
		if ( ! $this->permission->granted($this->user, 'add', $type)) {
			return $this->error(__('error_no_permission'));
		}
		// load model and prepare
		$this->loadEnvironment($type);
		// Did user submit data?
		if ($this->input->submit) {
			// yes
			// add a new record of type from the given form data
			$this->graph();
			try {
				R::store($this->record);
				R::tag($this->record, $this->record->autoTags());
				R::store($this->entry);
				R::associate($this->record, $this->entry);
				$this->user->autoBlog($this->record, 'add');
				// redirect somewhere better or stay here to add another record?
				if ($this->input->submit == __('editor_submit_add_cont')) {
					// continue adding
					$this->redirect(ahref(sprintf('%s/editor/add/%s/', $this->registry->language, $type)));					
				} else {
					// go to edit
					$this->redirect(ahref(sprintf('%s/editor/edit/%s/%s/', $this->registry->language, $type, $this->record->getId())));
				}
			} catch (Exception $e) {
				$this->data['errors'][] = __('editor_add_failed');
				$this->data['errors'] = array_merge($this->data['errors'], $this->record->error());
			}
		}
		$this->addBreadcrumbs('add');
		$this->toolbarActions('add', array('index', 'edit', 'view', 'delete'), true);
		// output the view
		$this->data['url'] = ahref(sprintf('%s/editor/add/%s/', $this->registry->language, $type));
		$this->data['submit'] = __('editor_submit_add');
		$this->data['secondary_submit'] = __('editor_submit_add_cont');
		$this->template->view('shared/editor/add', $this->data);
	}
	
	/**
	 * displays a form to edit a bean.
	 *
	 * @param string $type
	 * @param mixed $id
	 * @param int $limitOffset
	 * @param int $limitFind
	 */
	public function edit($type, $id, $limitOffset = 0, $limitFind = 24)
	{
		if ( ! $this->permission->granted($this->user, 'edit', $type, $id)) {
			return $this->error(__('error_no_permission'));
		}
		$this->data['limitOffset'] = $this->limitOffset = $limitOffset;
		$this->data['limitFind'] = $this->limitFind = $limitFind;
		// load model and prepare
		$this->loadEnvironment($type, $id);
		if ( ! $this->record->getId() == $id) {
			$this->redirect(ahref(sprintf('%s/editor/index/%s/', $this->registry->language, $type)));
		}
		// Did user submit data?
		if ($this->input->submit) {
			// yes
			// add a new record of type from the given form data
			$this->graph();
			try {
				R::store($this->record);
				R::tag($this->record, $this->record->autoTags());
				R::store($this->entry);
				R::associate($this->record, $this->entry);
				// rediret somewhere better or stay here to add another record?
				if ($this->input->submit == __('editor_submit_delete')) {
					// continue adding
					$this->redirect(ahref(sprintf('%s/editor/delete/%s/%s/', $this->registry->language, $type, $this->record->getId())));					
				} else {
					$this->user->autoBlog($this->record, 'edit');
					$this->redirect(ahref(sprintf('%s/editor/view/%s/%s/%s/%s/', $this->registry->language, $type, $this->record->getId(), $this->limitOffset, $this->limitFind)));
				}
			} catch (Exception $e) {
				$this->data['errors'][] = __('editor_edit_failed');
				$this->data['errors'] = array_merge($this->data['errors'], $this->record->error());
			}
		}
		$this->addBreadcrumbs('edit');
		$this->toolbarActions('edit', array('index'));
		// output the view
		$this->data['url'] = ahref(sprintf('%s/editor/edit/%s/%s/%s/%s/', $this->registry->language, $type, $id, $this->limitOffset, $this->limitFind));
		$this->data['submit'] = __('editor_submit_edit');
		//$this->data['secondary_submit'] = __('editor_submit_delete');
		$this->sniffPrevNext();
		// output the view
		$this->template->view('shared/editor/edit', $this->data);
	}

	/**
	 * displays the bean in read-only mode.
	 *
	 * @param string $type
	 * @param mixed $id
	 * @param int $limitOffset
	 * @param int $limitFind
	 */
	public function view($type, $id, $limitOffset = 0, $limitFind = 24)
	{
		if ( ! $this->permission->granted($this->user, 'view', $type, $id)) {
			return $this->error(__('error_no_permission'));
		}	
		$this->data['limitOffset'] = $this->limitOffset = $limitOffset;
		$this->data['limitFind'] = $this->limitFind = $limitFind;
		$this->loadEnvironment($type, $id);
		if ( ! $this->record->getId() == $id) {
			$this->redirect(ahref(sprintf('%s/editor/index/%s/', $this->registry->language, $type)));
		}
		
		if ($this->input->submit) {
			$this->redirect(ahref(sprintf('%s/editor/index/%s/', $this->registry->language, $type, $this->record->getId())));
		}
		$this->addBreadcrumbs('view');
		$this->toolbarActions('view', array('index')); // tools, but not index
		$this->data['url'] = ahref(sprintf('%s/editor/view/%s/%s/%s/%s/', $this->registry->language, $type, $id, $this->limitOffset, $this->limitFind));		
		$this->data['errors'] = array_merge($this->data['errors'], $this->record->error());
		$this->data['submit'] = __('editor_submit_view');
		$this->sniffPrevNext();
		// output the view
		$this->template->view('shared/editor/view', $this->data);
	}
	
	/**
	 * display a form to confirm a bean has to be deleted.
	 *
	 * @param string $type
	 * @param mixed $id
	 */
	public function delete($type, $id)
	{
		if ( ! $this->permission->granted($this->user, 'delete', $type, $id)) {
			return $this->error(__('error_no_permission'));
		}
		// load model and prepare
		$this->loadEnvironment($type, $id);
		
		if ($this->input->submit) {
			try {
				R::trash($this->record);
				$this->user->autoBlog($this->record, 'delete');
				$this->redirect(ahref(sprintf('%s/editor/index/%s/', $this->registry->language, $type)));	
			} catch (Exception $e) {
					$this->data['errors'][] = __('editor_delete_failed');
					$this->data['errors'] = array_merge($this->data['errors'], $this->record->error());
			}
		}
		$this->addBreadcrumbs('delete');
		$this->toolbarActions('delete', array('index'));
		$this->data['url'] = ahref(sprintf('%s/editor/delete/%s/%s', $this->registry->language, $type, $id));
		$this->data['submit'] = __('editor_submit_delete');
		// output the view
		$this->template->view('shared/editor/delete', $this->data);
	}
}
?>